<?php
/* 
 * class.myshopBase.php
 * 我的百宝箱基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-29
 */
class myshopBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
        $this->prompt = "";
    }

    //错误跳转
    function jumpError($error="", $pagetitle="", $backurl="" ){
    	$errors = $error ? $error : $this->error;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-错误提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	$content = $errors;
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //提示跳转
    function jumpPrompt($prompt="", $pagetitle="", $backurl=""){
        $prompts = $prompt ? $prompt : $this->prompt;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-信息提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $prompts);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //获取用户拥有的优惠券
    function getUserTicket($tid="", $user_id="", $seller_id="", $count=false){
        if(!$tid && !$user_id && !$seller_id) return false;
        if($tid) $append_tid = " AND `TicketID`='".$tid."'";
        if($user_id) $append_userid = " AND `UserID`='".$user_id."'";
        if($seller_id) $append_sellerid = " AND `SellerID`='".$seller_id."'";
        if($count){
            $sql = "SELECT count(`TicketID`) FROM `fun_ticket` WHERE 1 AND `isSet`=1 ".$append_tid.$append_userid.$append_sellerid;
            $count = $this->core->db->get_var($sql);
            return $count;
        }else{
            $sql = "SELECT * FROM `fun_ticket` WHERE 1 AND `isSet`=1 ".$append_tid.$append_userid.$append_sellerid;
            //echo $sql;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            if($tid) return $arr_infos[0];
            return $arr_infos;
        }
    }
    
    //获取商家地区、类别、商圈
    function getDistinctSellerCate($sellers, $distinct){
        if(!$sellers || !$distinct) return false;
        $seller_ids = "";
        if(is_array($sellers)) $seller_ids = @implode(',', $sellers);
        if(strpos($sellers, ',') !== false) $seller_ids = $sellers;
        
        $distinct_table = $distinct['table'];
        $distinct_field = $distinct['field'];
        $cate_id = $distinct['id'];
        $cate_name = $distinct['name'];
        
        $sql = "SELECT distinct(`".$distinct_field."`) FROM `fun_seller` WHERE 1 AND `SellerID` IN (".$seller_ids.")";
        $res = $this->core->db->get_results($sql);
        if(!$res) return false;
        $arr_cates = array();
        if($res){
            foreach($res as $key => $value){
                foreach($value as $k => $v){
                    $arr_cates[$key] = $v;
                }
            }
        }
        $str_cates = implode(',', $arr_cates);
        $sql = "SELECT `$cate_id`, `$cate_name` FROM `$distinct_table` WHERE 1 AND `$cate_id` IN ($str_cates)";
        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }
        return $arr_infos;
    }
}
?>
